package com.zhou.bjgl.bjgl.business_web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import com.zhou.bjgl.bjgl.business_web.entity.BStudent;
import com.zhou.bjgl.bjgl.business_web.entity.BUserRole;
import com.zhou.bjgl.bjgl.business_web.mapper.BUserRoleMapper;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.business_web.mapper.BStudentMapper;
import com.zhou.bjgl.bjgl.req.b_student.BStudentQueryReq;
import com.zhou.bjgl.bjgl.resp.student.BStudentQueryResp;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.business_web.service.BStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.bjgl.bjgl.util.CopyUtil;
import com.zhou.bjgl.bjgl.util.coder.SHA256Encrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

import static cn.hutool.setting.profile.Profile.DEFAULT_PROFILE;
import static com.zhou.bjgl.bjgl.constants.WxConstant.MY_DEFAULT_PROFILE;

/**
 * <p>
 * 学生servcie实现类
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@Service
public class BStudentServiceImpl extends ServiceImpl<BStudentMapper, BStudent> implements BStudentService {

    @Resource
    private BStudentMapper bStudentMapper;

    @Resource
    private BUserRoleMapper bUserRoleMapper;

    /**
     * 添加学生
     *
     * @param req
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BStudentQueryReq req) {
        BStudent bStudent = CopyUtil.copy(req, BStudent.class);
        //对密码进行加密
        if (!ObjectUtils.isEmpty(bStudent.getPassword())) {
            bStudent.setPassword(SHA256Encrypt.getSHA256StrJava(bStudent.getPassword()));
        }else {
            bStudent.setPassword(null);
        }
        //判断是添加还是修改
        if (ObjectUtils.nullSafeEquals("add", req.getTypeForEdit())) {
            //设置默认头像
            bStudent.setProfileUrl(MY_DEFAULT_PROFILE);
            //检查用户名或者密码是否为空
            if (StringUtils.isNullOrEmpty(req.getUsername())) {
                throw new BwException(BwExceptionEnum.NEED_USER_NAME);
            }
            if (StringUtils.isNullOrEmpty(req.getPassword())) {
                throw new BwException(BwExceptionEnum.NEED_PASSWORD);
            }

            int insert = bStudentMapper.insert(bStudent);

            //进行用户权限设置
            BUserRole bUserRole = new BUserRole();
            bUserRole.setRoleId(req.getPower());
            bUserRole.setUserId(bStudent.getId());
            bUserRoleMapper.insert(bUserRole);
            if (insert == 0) {
                throw new BwException(BwExceptionEnum.CREATE_FAIL);
            }
        } else {
            bStudentMapper.updateById(bStudent);
        }

    }


    /**
     * 查询学生列表
     *
     * @param req
     * @return
     */
    @Override
    public PageResp<BStudentQueryResp> queryStu(BStudentQueryReq req) {

        //进行分页
        PageHelper.startPage(req.getPage(), req.getSize());
        //查询记录
        List<BStudentQueryResp> studentRespList = bStudentMapper.queryStu(req);

        PageResp<BStudentQueryResp> pageResp = new PageResp<>();
        pageResp.setPageList(studentRespList);

        //获取总页数
        PageInfo<BStudentQueryResp> respPageInfo = new PageInfo<>(studentRespList);
        long total = respPageInfo.getTotal();
        pageResp.setTotal(total);

        return pageResp;
    }

    /**
     * 删除学生
     *
     * @param req
     */
    @Override
    public void deleteStu(BStudentQueryReq req) {
        try {
            int i = bStudentMapper.deleteById(req);


        } catch (Exception e) {
            e.printStackTrace();
            throw new BwException(BwExceptionEnum.DELETE_FAILID);
        }
    }
}
